import axios from "axios";
const axiosInstance = axios.create({
  baseURL: "/api",
  timeout: 1000,
});

// 请求拦截器
axiosInstance.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem("token");
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    // else {
    //   throw new Error("Token not Found");
    // }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
axiosInstance.interceptors.response.use(
  (response) => {
    // 请求响应成功的处理
    return response;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 封装get的请求方式
const get = (url, params = {}, options = {}) => {
  return axiosInstance.get(url, { params, ...options });
};

// 封装post的请求方式
const post = (url, data = {}, options = {}) => {
  return axiosInstance.post(url, data, options);
};

export { get, post, axiosInstance };
